<?php
namespace Admin\Model;
use Think\Model;
/**
 * 用户模型
 */
class AdminModel extends Model{
	

	//自动验证
	public $_validate = array(
		array('user','require','用户名必须填写'),
		array('password','require','密码必须填写'),
		array('code','require','验证码必须填写'),
	);
	
	//后台用户登录
	public function adminLogin(){
		//获取用户输入验证码
		$code = $_POST['code'];
		//实例化验证码类
		$verify = new \Think\Verify();
		//验证用户验证码
		$bool = $verify->check($code, $id);
		if($bool){
			//调用自动验证
			if(!$this->create()){
				//return false;
			}
			$user = I('post.user','','strip_tags');
			$password = I('post.password','','strip_tags');
			//定义要查询字段
			$condition['user_name'] = $user;
			//根据用户名查询用户信息
			$userInfo = M("admin")->where($condition)->find();
			//如果查询返回不为空 就匹配账号密码
			if($userInfo){
				//是否管理员
//				if($userInfo['is_admin'] >= 1){
					//判断用户信息是否匹配	 
					if($userInfo['passwd'] == md5($password)){
						if($userInfo['is_lock'] == 1){
							$this->error='登录失败，账号禁用';
							return false;
						}
						$_SESSION['uid'] = $userInfo['uid'];
						$_SESSION['aid'] = $userInfo['uid'];
						$_SESSION['user_name'] = $userInfo['user_name'];
						//保存登录信息
						$data['uid'] = $userInfo['uid'];	//用户ID
						$data['login_ip'] = get_client_ip();	//最后登录IP
						$data['login_time'] = time();		//最后登录时间
						$data['login_count'] = $userInfo['logincount'] + 1;

						$res = M('admin')->save($data);
						return true;
					}else{
						$this->error='用户名密码不正确';
						return false;
					}
//				}else{
//					$this->error='权限不足';//不是管理员
//					return false;
//				}
			}else{
				$this->error='用户名或密码错误';//账号不存在
				return false;
			}
		}else{
			$this->error='验证码不正确';
			return false;
		}		
	}
}
